package net.wesley.android.city;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Handler;
import android.os.Message;
import android.util.Log;
import net.wesley.android.City;
import net.yebaihe.sdk.HttpConnection;

public class DaLian extends City {

	private Handler requesthandler=new Handler(){
		public void handleMessage(Message message){
			switch (message.what){
			case HttpConnection.DID_ERROR://DID ERROR
				callback.onComplete(-1, "");
				break;
			case HttpConnection.DID_SUCCEED://DID SUCC
				String body=(String)message.obj;
				//Log.d("myown", body);
				try {
					JSONArray jsonObject=new JSONArray(body);
					JSONObject obj= (JSONObject) jsonObject.opt(0);
					String retvalue="";
					if (obj.getString("flag").equals("yes")){
						JSONArray wzs=obj.getJSONArray("data");
						for (int i=0;i<wzs.length();i++){
							JSONObject obj2=wzs.optJSONObject(i);
							retvalue+=String.format("shijian:%s\ndidian:%s\ncode:%s\n\n",obj2.getString("WFSJ"),obj2.getString("WFDZ"),obj2.getString("WZYY"));
						}
						callback.onComplete(wzs.length(), retvalue);
					}
					else{
						callback.onComplete(0, "");
					}
				} catch (JSONException e) {
					callback.onComplete(-10001, "");
				}
				break;
			}
		}
	};

	public void requestParamsFinish(String[] values) {
		try {
			new HttpConnection(ctx,requesthandler).get("http://www.dlutc.gov.cn/getinfo.php?hm="+URLEncoder.encode("辽","UTF-8")+chepai.substring(1)+"&hpzl=02&action=0");
		} catch (UnsupportedEncodingException e) {
			callback.onComplete(-10002, "");
		}
	}

	@Override
	public boolean match(String chepai) {
		return chepai.startsWith("辽B");
	}

	@Override
	public String[] paramDesc() {
		return null;
	}

}
